package com.turing.post.cardmanagement.util;

import java.io.Serializable;

import com.turing.post.util.PCPSUtil;

/**
 * 卡应用发行信息文件(应用目录/0015)
 * 
 * @author yxb
 * 
 */
@SuppressWarnings("serial")
public class CardAppIssueFile implements Serializable {
	/**
	 * 01－01 应用版本(当前版本: 0x01) 1 BCD
	 */
	private String appVersion = APPVERSION_01;

	public static final String APPVERSION_01 = "01";

	public void setAppVersion(String appVersion) {
		this.appVersion = appVersion;
	}

	public String getAppVersion() {
		return appVersion;
	}

	/**
	 * 02－04 发卡方代码(实际6 ASC) 3 BCD
	 */
	private String issueCompanyNo;

	public void setIssueCompanyNo(String issueCompanyNo) {
		this.issueCompanyNo = issueCompanyNo;
	}

	public String getIssueCompanyNo() {
		return issueCompanyNo;
	}

	/**
	 * 05－13 卡序号(18 ASC) 9 BCD
	 */
	private String cardSeqNo;

	public void setCardSeqNo(String cardSeqNo) {
		this.cardSeqNo = cardSeqNo;
	}

	public String getCardSeqNo() {
		return cardSeqNo;
	}

	/**
	 * 14－21 应用序列号(发卡商3+应用发行顺序号5) 8 HEX
	 */
	private String appSeqNo;

	public void setAppSeqNo(String appSeqNo) {
		this.appSeqNo = appSeqNo;
	}

	public String getAppSeqNo() {
		return appSeqNo;
	}

	/**
	 * 22－25 应用发行日期 4 YYYYMMDD
	 */
	private String appIssueDate;

	public void setAppIssueDate(String appIssueDate) {
		this.appIssueDate = appIssueDate;
	}

	public String getAppIssueDate() {
		return appIssueDate;
	}

	/**
	 * 将应用发行信息编码成16进制字符串以准备写卡
	 * 
	 * @return
	 */
	public String encodeCardAppIssueFile() {
		StringBuilder sb = new StringBuilder();
		// 此处应检查数据的合法性
		// 版本号
		sb.append(APPVERSION_01);
		// 发卡商号
		sb.append(getIssueCompanyNo());
		// 卡序号
		sb.append(getCardSeqNo());
		// 应用序列号
		sb.append(getAppSeqNo());
		// 应用发行日期
		sb.append(getAppIssueDate());

		return sb.toString();
	}

	/**
	 * 解析HEX字符串内容到应用发行基本信息对象
	 * 
	 * @param hexstr
	 * @return
	 */
	public static CardAppIssueFile parseCardAppIssueFile(String hexstr) {
		if (PCPSUtil.isEmpty(hexstr))
			return null;

		if (hexstr.length() < 50)
			return null;

		CardAppIssueFile caif = new CardAppIssueFile();

		// 解析appVersion
		caif.setAppVersion(hexstr.substring(0, 2));

		// 解析issueCompanyNo
		caif.setIssueCompanyNo(hexstr.substring(2, 8));

		// 解析cardNo
		caif.setCardSeqNo(hexstr.substring(8, 26));

		// 解析appSeqNo
		caif.setAppSeqNo(hexstr.substring(26, 42));

		// 解析appIssueDate
		caif.setAppIssueDate(hexstr.substring(42, 50));

		return caif;
	}
}
